package util;

import java.sql.SQLException;
import java.util.*;

import model.*;
import dal.*;

public class CacheUtil {

	public static void addToCache(IModel obj) throws Exception {
		if (obj instanceof AgendaItemModel) { // AgendaItem
			_agendaItemChanged = true;
			// ModelUtil.addToList(_agendaItemList, (AgendaItemModel) obj);
		} else if (obj instanceof AgendaModel) { // Agenda
			_agendaChanged = true;
			// ModelUtil.addToList(_agendaList, (AgendaModel) obj);
		} else if (obj instanceof ChecklistModel) { // Checklist
			_checklistChanged = true;
			// ModelUtil.addToList(_checklistList, (ChecklistModel) obj);
		} else if (obj instanceof ChecklistItemModel) { // ChecklistItem
			_checklistItemChanged = true;
			// ModelUtil.addToList(_checklistItemList, (ChecklistItemModel)
			// obj);
		} else if (obj instanceof QuotationModel) { // Quotaion
			_quotationChanged = true;
			// ModelUtil.addToList(_quotationList, (QuotationModel) obj);
		} else if (obj instanceof QuotationItemModel) { // QuotationItem
			_quotationItemChanged = true;
			// ModelUtil.addToList(_quotationItemList, (QuotationItemModel)
			// obj);
		} else if (obj instanceof EventModel) { // Event
			_eventChanged = true;
			// ModelUtil.addToList(_eventList, (EventModel) obj);
		} else if (obj instanceof ProjectModel) { // Project
			_projectChanged = true;
			// ModelUtil.addToList(_projectList, (ProjectModel) obj);
		} else if (obj instanceof CompanyModel) { // Company
			_companyChanged = true;
			// ModelUtil.addToList(_companyList, (CompanyModel) obj);
		} else if (obj instanceof EmployeeModel) { // Employee
			_employeeChanged = true;
			// ModelUtil.addToList(_employeeList, (EmployeeModel) obj);
		} else if (obj instanceof UnitModel) { // Unit
			_unitChanged = true;
			// ModelUtil.addToList(_unitList, (UnitModel) obj);
		} else if (obj instanceof VenueModel) { // Venue
			_venueChanged = true;
			// ModelUtil.addToList(_venueList, (VenueModel) obj);
		} else if (obj instanceof ProjectTypeModel) { // ProjectType
			_projectTypeChanged = true;
			// ModelUtil.addToList(_projectTypeList, (ProjectTypeModel) obj);
		} else if (obj instanceof PhaseModel) { // PhaseModel
			_phaseChanged = true;
		} else {
			// TODO more class here
		}
	}

	public static void updateCache(IModel obj) throws SQLException, ClassNotFoundException {
		if (obj instanceof AgendaModel) {
			_agendaChanged = true;
			// ModelUtil.updateList(_agendaList, (AgendaModel) obj);
		} else if (obj instanceof AgendaItemModel) {
			_agendaItemChanged = true;
			// ModelUtil.updateList(_agendaItemList, (AgendaItemModel) obj);
		} else if (obj instanceof ChecklistModel) {
			_checklistChanged = true;
			// ModelUtil.updateList(_checklistList, (ChecklistModel) obj);
		} else if (obj instanceof ChecklistItemModel) {
			_checklistItemChanged = true;
			// ModelUtil.updateList(_checklistItemList, (ChecklistItemModel)
			// obj);
		} else if (obj instanceof QuotationModel) {
			_quotationChanged = true;
			// ModelUtil.updateList(_quotationList, (QuotationModel) obj);
		} else if (obj instanceof QuotationItemModel) {
			_quotationItemChanged = true;
			// ModelUtil.updateList(_quotationItemList, (QuotationItemModel)
			// obj);
		} else if (obj instanceof CompanyModel) {
			_companyChanged = true;
			// ModelUtil.updateList(_companyList, (CompanyModel) obj);
		} else if (obj instanceof EmployeeModel) {
			_employeeChanged = true;
			// ModelUtil.updateList(_employeeList, (EmployeeModel) obj);
		} else if (obj instanceof EventModel) {
			_eventChanged = true;
			// ModelUtil.updateList(_eventList, (EventModel) obj);
		} else if (obj instanceof ProjectModel) {
			_projectChanged = true;
			// ModelUtil.updateList(_projectList, (ProjectModel) obj);
		} else if (obj instanceof ProjectTypeModel) {
			_projectTypeChanged = true;
			// ModelUtil.updateList(_projectTypeList, (ProjectTypeModel) obj);
		} else if (obj instanceof UnitModel) {
			_unitChanged = true;
			// ModelUtil.updateList(_unitList, (UnitModel) obj);
		} else if (obj instanceof VenueModel) {
			_venueChanged = true;
			// ModelUtil.removeFromList(_venueList, (VenueModel) obj);
			// ModelUtil.updateList(_venueList, (VenueModel) obj);
		} else {
			// TODO more class here
		}
	}

	public static void deleteCache(IModel obj) throws SQLException, ClassNotFoundException {
		if (obj instanceof AgendaModel) {
			ModelUtil.removeFromList(_agendaList, (AgendaModel) obj);
		} else if (obj instanceof AgendaItemModel) {
			ModelUtil.removeFromList(_agendaItemList, (AgendaItemModel) obj);
		} else if (obj instanceof ChecklistModel) {
			ModelUtil.removeFromList(_checklistList, (ChecklistModel) obj);
		} else if (obj instanceof ChecklistItemModel) {
			ModelUtil.removeFromList(_checklistItemList, (ChecklistItemModel) obj);
		} else if (obj instanceof QuotationModel) {
			ModelUtil.removeFromList(_quotationList, (QuotationModel) obj);
		} else if (obj instanceof QuotationItemModel) {
			ModelUtil.removeFromList(_quotationItemList, (QuotationItemModel) obj);// ///////////////////////////
		} else if (obj instanceof CompanyModel) {
			ModelUtil.removeFromList(_companyList, (CompanyModel) obj);
		} else if (obj instanceof EmployeeModel) {
			ModelUtil.removeFromList(_employeeList, (EmployeeModel) obj);
		} else if (obj instanceof EventModel) {
			ModelUtil.removeFromList(_eventList, (EventModel) obj);
		} else if (obj instanceof ProjectModel) {
			ModelUtil.removeFromList(_projectList, (ProjectModel) obj);
		} else if (obj instanceof ProjectTypeModel) {
			ModelUtil.removeFromList(_projectTypeList, (ProjectTypeModel) obj);
		} else if (obj instanceof UnitModel) {
			ModelUtil.removeFromList(_unitList, (UnitModel) obj);
		} else if (obj instanceof VenueModel) {
			ModelUtil.removeFromList(_venueList, (VenueModel) obj);
		} else {
			// TODO more class here
		}
	}

	// PROJECT TYPE

	private static List<ProjectTypeModel> _projectTypeList = null;
	private static boolean _projectTypeChanged = false;

	private static void loadProjectTypeList() throws Exception {
		if (_projectTypeList == null || _projectTypeChanged) {
			GenericDal<ProjectTypeModel, ProjectTypeModelExt> dalProjectType = new GenericDal<ProjectTypeModel, ProjectTypeModelExt>(new ProjectTypeModelExt());
			ProjectTypeModelExt queryProjectType = new ProjectTypeModelExt();
			_projectTypeList = dalProjectType.selectList(queryProjectType);
			_projectTypeChanged = false;
		}
	}

	public static List<ProjectTypeModel> getProjectTypeList() throws Exception {
		loadProjectTypeList();
		// TODO : sort
		return _projectTypeList;
	}

	public static List<ProjectTypeModel> getProjectTypeList(boolean sorted, String sortField) throws Exception {
		loadProjectTypeList();
		// TODO : sort
		return _projectTypeList;
	}

	// END OF PROJECT TYPE

	// COMPANY

	private static List<CompanyModel> _companyList = null;
	private static boolean _companyChanged = false;

	private static void loadCompanyList() throws Exception {
		if (_companyList == null || _companyChanged) {
			GenericDal<CompanyModel, CompanyModelExt> dalCompany = new GenericDal<CompanyModel, CompanyModelExt>(new CompanyModelExt());
			CompanyModelExt queryCompany = new CompanyModelExt();
			_companyList = dalCompany.selectList(queryCompany);
			_companyChanged = false;
		}
	}

	public static List<CompanyModel> getCompanyList() throws Exception {
		loadCompanyList();
		// TODO : sort
		return _companyList;
	}

	public static List<CompanyModel> getCompanyList(boolean sorted, String sortField) throws Exception {
		loadCompanyList();
		// TODO : sort
		return _companyList;
	}

	public static String getCompanyName(int idCompany) throws Exception {
		String name = "";
		loadCompanyList();
		CompanyModel obj = ModelUtil.<CompanyModel> getFromList(_companyList, idCompany);
		if (obj != null && obj.getName() != null) {
			name = obj.getName();
		}
		return name;
	}

	// END OF COMPANY

	// PHASE begin

	private static List<PhaseModel> _phaseList = null;
	private static boolean _phaseChanged = false;

	public static List<PhaseModel> getPhaseList(int idProject) throws Exception {
		PhaseModelExt query = new PhaseModelExt();
		query.setIdProject(idProject);

		GenericDal<PhaseModel, PhaseModelExt> dal = new GenericDal<PhaseModel, PhaseModelExt>(new PhaseModelExt());
		List<PhaseModel> list = dal.selectList(query);
		return list;
	}

	private static void loadPhaseList() throws Exception {
		if (_phaseList == null || _phaseChanged) {

			GenericDal<PhaseModel, PhaseModelExt> dalPhase = new GenericDal<PhaseModel, PhaseModelExt>(new PhaseModelExt());
			PhaseModelExt queryPhase = new PhaseModelExt();

			if (_phaseList == null) {
				// first time
				_phaseList = dalPhase.selectList(queryPhase);
			} else if (_phaseChanged) {
				Date lastUpdated = ModelUtil.getLastUpdated(_phaseList);

				// load records that have changed after the last updated
				queryPhase.setUpdatedFrom(lastUpdated);

				List<PhaseModel> list = dalPhase.selectList(queryPhase);

				// update new or changed records to cache
				ModelUtil.updateList(_phaseList, list);
			}

			// set vanue changed as false
			_phaseChanged = false;
		}
	}

	public static List<PhaseModel> getPhaseList() throws Exception {
		loadPhaseList();
		return _phaseList;
	}

	public static List<PhaseModel> getPhaseList(boolean sorted, Comparator<PhaseModel> comparator) throws Exception {
		loadPhaseList();
		Collections.sort(_phaseList, comparator);
		return _phaseList;
	}

	public static String getPhaseName(int idPhase) throws Exception {
		String name = "";
		loadPhaseList();
		PhaseModel obj = ModelUtil.<PhaseModel> getFromList(_phaseList, idPhase);
		if (obj != null && obj.getName() != null) {
			name = obj.getName();
		}
		return name;
	}

	// PHASE end

	// VENUE begin

	private static List<VenueModel> _venueList = null;
	private static boolean _venueChanged = false;

	private static void loadVenueList() throws Exception {
		if (_venueList == null || _venueChanged) {

			GenericDal<VenueModel, VenueModelExt> dalVenue = new GenericDal<VenueModel, VenueModelExt>(new VenueModelExt());
			VenueModelExt queryVenue = new VenueModelExt();

			if (_venueList == null) {
				// first time
				_venueList = dalVenue.selectList(queryVenue);
			} else if (_venueChanged) {
				Date lastUpdated = ModelUtil.getLastUpdated(_venueList);

				// load records that have changed after the last updated
				queryVenue.setUpdatedFrom(lastUpdated);

				List<VenueModel> list = dalVenue.selectList(queryVenue);

				// update new or changed records to cache
				ModelUtil.updateList(_venueList, list);
			}

			// set vanue changed as false
			_venueChanged = false;
		}
	}

	public static List<VenueModel> getVenueList() throws Exception {
		loadVenueList();
		return _venueList;
	}

	public static List<VenueModel> getVenueList(boolean sorted, Comparator<VenueModel> comparator) throws Exception {
		loadVenueList();
		Collections.sort(_venueList, comparator);
		return _venueList;
	}

	public static String getVenueName(int idVenue) throws Exception {
		String name = "";
		loadVenueList();
		VenueModel obj = ModelUtil.<VenueModel> getFromList(_venueList, idVenue);
		if (obj != null && obj.getName() != null) {
			name = obj.getName();
		}
		return name;
	}

	// VENUE end

	// Quotation
	private static List<QuotationModel> _quotationList = null;
	private static boolean _quotationChanged = false;

	private static void loadQuotationList() throws Exception {
		if (_quotationList == null || _quotationChanged) {
			GenericDal<QuotationModel, QuotationModelExt> dalQuotation = new GenericDal<QuotationModel, QuotationModelExt>(new QuotationModelExt());
			QuotationModelExt queryQuotation = new QuotationModelExt();
			_quotationList = dalQuotation.selectList(queryQuotation);
			_quotationChanged = false;
		}
	}

	public static List<QuotationModel> getQuotationList() throws Exception {
		loadQuotationList();
		// TODO : sort
		return _quotationList;
	}

	public static List<QuotationModel> getQuotationList(boolean sorted, String sortField) throws Exception {
		loadQuotationList();
		// TODO : sort
		return _quotationList;
	}

	public static String getQuotationName(int idQuotation) throws Exception {
		String name = "";
		loadQuotationList();
		QuotationModel obj = ModelUtil.<QuotationModel> getFromList(_quotationList, idQuotation);
		if (obj != null && obj.getName() != null) {
			name = obj.getName();
		}
		return name;
	}

	public static QuotationModel getQuotation(int idQuotation) throws Exception {
		loadQuotationList();
		QuotationModel obj = ModelUtil.<QuotationModel> getFromList(_quotationList, idQuotation);
		return obj;
	}

	// End of Quotation

	// QuotationItem
	private static List<QuotationItemModel> _quotationItemList = null;
	private static boolean _quotationItemChanged = false;

	private static void loadQuotationItemList() throws Exception {
		if (_quotationItemList == null || _quotationItemChanged) {
			GenericDal<QuotationItemModel, QuotationItemModelExt> dalQuotationItem = new GenericDal<QuotationItemModel, QuotationItemModelExt>(new QuotationItemModelExt());
			QuotationItemModelExt queryQuotationItem = new QuotationItemModelExt();
			_quotationItemList = dalQuotationItem.selectList(queryQuotationItem);
			_quotationItemChanged = false;
		}
	}

	public static List<QuotationItemModel> getIsGroupQuotationItemList(int idQuotation) throws Exception {
		QuotationItemModelExt query = new QuotationItemModelExt();
		query.setIdQuotation(idQuotation);
		query.setIsGroup(true);
		GenericDal<QuotationItemModel, QuotationItemModelExt> dal = new GenericDal<QuotationItemModel, QuotationItemModelExt>(new QuotationItemModelExt());
		List<QuotationItemModel> list = dal.selectList(query);
		return list;
	}

	// public static List<QuotationItemModel> getQuotationItemList() throws
	// Exception {
	//
	// // loadQuotationItemList();
	//
	// // TODO : sort
	//
	// return _quotationItemList;
	// }
	//
	// public static List<QuotationItemModel> getQuotationItemList(boolean
	// sorted, String sortField) throws Exception {
	//
	// // loadQuotationItemList();
	//
	// // TODO : sort
	//
	// return _quotationItemList;
	// }

	public static String getQuotationItemName(int idQuotationItem) throws Exception {
		loadQuotationItemList();
		String name = "";
		QuotationItemModel obj = ModelUtil.<QuotationItemModel> getFromList(_quotationItemList, idQuotationItem);
		if (obj != null && obj.getName() != null) {
			name = obj.getName();
		}
		return name;
	}

	// End of QuotationItem

	// Product
	private static List<ProductModel> _productList = null;
	private static boolean _productChanged = false;

	private static void loadProductList() throws Exception {
		if (_productList == null || _productChanged) {
			GenericDal<ProductModel, ProductModelExt> dalProduct = new GenericDal<ProductModel, ProductModelExt>(new ProductModelExt());
			ProductModelExt queryProduct = new ProductModelExt();
			_productList = dalProduct.selectList(queryProduct);
			_productChanged = false;
		}
	}

	public static List<ProductModel> getProductList() throws Exception {
		loadProductList();
		// TODO : sort
		return _productList;
	}

	public static List<ProductModel> getProductList(boolean sorted, String sortField) throws Exception {
		loadProductList();
		// TODO : sort
		return _productList;
	}

	public static String getProductName(int idProduct) throws Exception {
		String name = "";
		loadProductList();
		ProductModel obj = ModelUtil.<ProductModel> getFromList(_productList, idProduct);
		if (obj != null && obj.getName() != null) {
			name = obj.getName();
		}
		return name;
	}

	// End of Product

	// UNIT

	private static List<UnitModel> _unitList = null;
	private static boolean _unitChanged = false;

	private static void loadUnitList() throws Exception {
		if (_unitList == null || _unitChanged) {
			GenericDal<UnitModel, UnitModelExt> dalUnit = new GenericDal<UnitModel, UnitModelExt>(new UnitModelExt());
			UnitModelExt queryUnit = new UnitModelExt();
			_unitList = dalUnit.selectList(queryUnit);
			_unitChanged = false;
		}
	}

	public static List<UnitModel> getUnitList() throws Exception {
		loadUnitList();
		// TODO : sort
		return _unitList;
	}

	public static List<UnitModel> getUnitList(boolean sorted, String sortField) throws Exception {

		loadUnitList();

		// TODO : sort

		return _unitList;
	}

	public static String getUnitName(int idUnit) throws Exception {
		String name = "";
		loadUnitList();
		UnitModel obj = ModelUtil.<UnitModel> getFromList(_unitList, idUnit);
		if (obj != null && obj.getName() != null) {
			name = obj.getName();
		}
		return name;
	}

	// END OF UNIT

	// SERVICE

	private static List<ServiceModel> _serviceList = null;
	private static boolean _serviceChanged = false;

	private static void loadServiceList() throws Exception {
		if (_serviceList == null || _serviceChanged) {
			GenericDal<ServiceModel, ServiceModelExt> dalService = new GenericDal<ServiceModel, ServiceModelExt>(new ServiceModelExt());
			ServiceModelExt queryService = new ServiceModelExt();
			_serviceList = dalService.selectList(queryService);
			_serviceChanged = false;
		}
	}

	public static List<ServiceModel> getServiceList() throws Exception {

		loadServiceList();

		// TODO : sort

		return _serviceList;
	}

	public static List<ServiceModel> getServiceList(boolean sorted, String sortField) throws Exception {

		loadServiceList();

		// TODO : sort

		return _serviceList;
	}

	public static String getServiceName(int idService) throws Exception {
		String name = "";
		loadServiceList();
		ServiceModel obj = ModelUtil.<ServiceModel> getFromList(_serviceList, idService);
		if (obj != null && obj.getName() != null) {
			name = obj.getName();
		}
		return name;
	}

	// END OF SERVICE

	// CURRENCY
	private static List<CurrencyModel> _currencyList = null;
	private static boolean _currencyChanged = false;

	private static void loadCurrencyList() throws Exception {
		if (_currencyList == null || _currencyChanged) {
			GenericDal<CurrencyModel, CurrencyModelExt> dalCurrency = new GenericDal<CurrencyModel, CurrencyModelExt>(new CurrencyModelExt());
			CurrencyModelExt queryCurrency = new CurrencyModelExt();
			_currencyList = dalCurrency.selectList(queryCurrency);
			_currencyChanged = false;
		}
	}

	public static List<CurrencyModel> getCurrencyList() throws Exception {

		loadCurrencyList();

		// TODO : sort

		return _currencyList;
	}

	public static List<CurrencyModel> getCurrencyList(boolean sorted, String sortField) throws Exception {

		loadCurrencyList();

		// TODO : sort

		return _currencyList;
	}

	public static String getCurrencyName(int idCurrency) throws Exception {
		String name = "";
		loadCurrencyList();
		CurrencyModel obj = ModelUtil.<CurrencyModel> getFromList(_currencyList, idCurrency);
		if (obj != null && obj.getName() != null) {
			name = obj.getName();
		}
		return name;
	}

	// END OF CURRENCY

	// PROJECT
	private static List<ProjectModel> _projectList = null;
	private static boolean _projectChanged = false;

	private static void loadProjectList() throws Exception {
		if (_projectList == null || _projectChanged) {
			GenericDal<ProjectModel, ProjectModelExt> dalProject = new GenericDal<ProjectModel, ProjectModelExt>(new ProjectModelExt());
			ProjectModelExt queryProject = new ProjectModelExt();
			queryProject.setOffset(Integer.MIN_VALUE);
			queryProject.setLimit(Integer.MIN_VALUE);

			_projectList = dalProject.selectList(queryProject);

			_projectChanged = false;
		}
	}

	public static List<ProjectModel> getProjectList() throws Exception {

		loadProjectList();

		// TODO : sort

		return _projectList;
	}

	public static List<ProjectModel> getProjectList(boolean sorted, String sortField) throws Exception {

		loadProjectList();

		// TODO : sort

		return _projectList;
	}

	public static String getProjectName(int idProject) throws Exception {
		String name = "";
		loadProjectList();
		ProjectModel obj = ModelUtil.<ProjectModel> getFromList(_projectList, idProject);
		if (obj != null && obj.getName() != null) {
			name = obj.getName();
		}
		return name;
	}

	// END OF PROJECT

	// EMPLOYEE
	private static List<EmployeeModel> _employeeList = null;
	private static boolean _employeeChanged = false;

	private static void loadEmployeeList() throws Exception {
		if (_employeeList == null || _employeeChanged) {
			GenericDal<EmployeeModel, EmployeeModelExt> dalEmployee = new GenericDal<EmployeeModel, EmployeeModelExt>(new EmployeeModelExt());
			EmployeeModelExt queryEmployee = new EmployeeModelExt();
			queryEmployee.setOffset(Integer.MIN_VALUE);
			queryEmployee.setLimit(Integer.MIN_VALUE);
			_employeeList = dalEmployee.selectList(queryEmployee);
			_employeeChanged = false;
		}
	}

	public static List<EmployeeModel> getEmployeeList() throws Exception {

		loadEmployeeList();

		// TODO : sort

		return _employeeList;
	}

	public static List<EmployeeModel> getEmployeeList(boolean sorted, String sortField) throws Exception {

		loadEmployeeList();

		// TODO : sort

		return _employeeList;
	}

	public static List<EmployeeModel> getPMList() throws Exception {

		loadEmployeeList();

		// TODO : sort

		return _employeeList;
	}

	public static String getEmployeeName(int idEmployee) throws Exception {
		String name = "";
		loadEmployeeList();
		EmployeeModel obj = ModelUtil.<EmployeeModel> getFromList(_employeeList, idEmployee);
		if (obj != null && obj.getName() != null) {
			name = obj.getName();
		}
		return name;
	}

	// END OF EMPLOYEE

	// Agenda
	private static List<AgendaModel> _agendaList = null;
	private static boolean _agendaChanged = false;

	private static void loadAgendaList() throws Exception {

		if (_agendaList == null || _agendaChanged) {
			GenericDal<AgendaModel, AgendaModelExt> dalAgenda = new GenericDal<AgendaModel, AgendaModelExt>(new AgendaModelExt());
			AgendaModelExt queryAgenda = new AgendaModelExt();
			queryAgenda.setOffset(Integer.MIN_VALUE);
			queryAgenda.setLimit(Integer.MIN_VALUE);

			_agendaList = dalAgenda.selectList(queryAgenda);

			_agendaChanged = false;
		}
	}

	public static List<AgendaModel> getAgendaList() throws Exception {

		loadAgendaList();

		// TODO : sort

		return _agendaList;
	}

	public static List<AgendaModel> getAgendaList(boolean sorted, String sortField) throws Exception {

		loadAgendaList();

		// TODO : sort

		return _agendaList;
	}

	public static String getAgendaName(int idAgenda) throws Exception {
		String name = "";
		loadAgendaList();
		AgendaModel obj = ModelUtil.<AgendaModel> getFromList(_agendaList, idAgenda);
		if (obj != null && obj.getName() != null) {
			name = obj.getName();
		}
		return name;
	}

	public static AgendaModel getAgenda(int idAgenda) throws Exception {
		loadAgendaList();
		AgendaModel obj = ModelUtil.<AgendaModel> getFromList(_agendaList, idAgenda);
		return obj;
	}

	// End of Agenda

	// AGENDAITEM
	private static List<AgendaItemModel> _agendaItemList = null;
	private static boolean _agendaItemChanged = false;

	private static void loadAgendaItemList() throws Exception {

		if (_agendaItemList == null || _agendaItemChanged) {
			GenericDal<AgendaItemModel, AgendaItemModelExt> dalAgendaItem = new GenericDal<AgendaItemModel, AgendaItemModelExt>(new AgendaItemModelExt());
			AgendaItemModelExt queryAgendaItem = new AgendaItemModelExt();
			queryAgendaItem.setOffset(Integer.MIN_VALUE);
			queryAgendaItem.setLimit(Integer.MIN_VALUE);

			_agendaItemList = dalAgendaItem.selectList(queryAgendaItem);

			_agendaItemChanged = false;
		}
	}

	public static List<AgendaItemModel> getAgendaItemList() throws Exception {

		loadAgendaItemList();

		// TODO : sort

		return _agendaItemList;
	}

	public static List<AgendaItemModel> getAgendaItemList(boolean sorted, String sortField) throws Exception {

		loadAgendaItemList();

		// TODO : sort

		return _agendaItemList;
	}

	public static List<AgendaItemModel> getIsGroupAgendaItemList(int iAgendaId) throws Exception {
		AgendaItemModelExt query = new AgendaItemModelExt();
		query.setIdAgenda(iAgendaId);
		query.setIsGroup(true);

		GenericDal<AgendaItemModel, AgendaItemModelExt> dal = new GenericDal<AgendaItemModel, AgendaItemModelExt>(new AgendaItemModelExt());
		List<AgendaItemModel> list = dal.selectList(query);
		return list;
	}

	public static List<AgendaItemModel> getSameParentAgendaItemList(int iParentId) throws Exception {
		AgendaItemModelExt query = new AgendaItemModelExt();
		query.setIdParent(iParentId);

		GenericDal<AgendaItemModel, AgendaItemModelExt> dal = new GenericDal<AgendaItemModel, AgendaItemModelExt>(new AgendaItemModelExt());
		List<AgendaItemModel> list = dal.selectList(query);
		return list;
	}

	public static String getAgendaItemName(int idAgendaItem) throws Exception {
		String name = "";
		loadAgendaItemList();
		AgendaItemModel obj = ModelUtil.<AgendaItemModel> getFromList(_agendaItemList, idAgendaItem);
		if (obj != null && obj.getName() != null) {
			name = obj.getName();
		}
		return name;
	}

	// END OF AGENDA ITEM

	// CHECKLIST
	private static List<ChecklistModel> _checklistList = null;
	private static boolean _checklistChanged = false;

	private static void loadChecklistList() throws Exception {

		if (_checklistList == null || _checklistChanged) {
			GenericDal<ChecklistModel, ChecklistModelExt> dalChecklist = new GenericDal<ChecklistModel, ChecklistModelExt>(new ChecklistModelExt());
			ChecklistModelExt queryChecklist = new ChecklistModelExt();
			queryChecklist.setOffset(Integer.MIN_VALUE);
			queryChecklist.setLimit(Integer.MIN_VALUE);

			_checklistList = dalChecklist.selectList(queryChecklist);

			_checklistChanged = false;
		}
	}

	public static List<ChecklistModel> getChecklistList() throws Exception {

		loadChecklistList();

		// TODO : sort

		return _checklistList;
	}

	public static List<ChecklistModel> getChecklistList(boolean sorted, String sortField) throws Exception {

		loadChecklistList();

		// TODO : sort

		return _checklistList;
	}

	public static String getChecklistName(int idChecklist) throws Exception {
		String name = "";

		loadChecklistList();
		ChecklistModel obj = ModelUtil.<ChecklistModel> getFromList(_checklistList, idChecklist);
		if (obj != null && obj.getName() != null) {
			name = obj.getName();
		}

		return name;
	}

	public static ChecklistModel getChecklist(int idChecklist) throws Exception {
		loadChecklistList();
		ChecklistModel obj = ModelUtil.<ChecklistModel> getFromList(_checklistList, idChecklist);
		return obj;
	}

	// END OF CHECKLIST

	// ChecklistItem
	private static List<ChecklistItemModel> _checklistItemList = null;// Dat
																		// added
	private static boolean _checklistItemChanged = false;// Dat added

	public static List<ChecklistItemModel> getIsGroupChecklistItemList(int iChecklistId) throws Exception {
		ChecklistItemModelExt query = new ChecklistItemModelExt();
		query.setIdChecklist(iChecklistId);
		query.setIsGroup(true);

		GenericDal<ChecklistItemModel, ChecklistItemModelExt> dal = new GenericDal<ChecklistItemModel, ChecklistItemModelExt>(new ChecklistItemModelExt());
		List<ChecklistItemModel> list = dal.selectList(query);
		return list;
	}

	// Dat added loadChecklistItemList and getChecklistItemName mehtod

	private static void loadChecklistItemList() throws Exception {

		if (_checklistItemList == null || _checklistItemChanged) {
			GenericDal<ChecklistItemModel, ChecklistItemModelExt> dalChecklistItem = new GenericDal<ChecklistItemModel, ChecklistItemModelExt>(new ChecklistItemModelExt());
			ChecklistItemModelExt queryChecklistItem = new ChecklistItemModelExt();
			queryChecklistItem.setOffset(Integer.MIN_VALUE);
			queryChecklistItem.setLimit(Integer.MIN_VALUE);

			_checklistItemList = dalChecklistItem.selectList(queryChecklistItem);

			_checklistItemChanged = false;
		}
	}

	public static String getChecklistItemName(int idChecklistItem) throws Exception {
		String name = "";

		loadChecklistItemList();
		ChecklistItemModel obj = ModelUtil.<ChecklistItemModel> getFromList(_checklistItemList, idChecklistItem);
		if (obj != null && obj.getName() != null) {
			name = obj.getName();
		}

		return name;
	}

	// End of ChecklistItem

	// EVENT

	private static List<EventModel> _eventList = null;
	private static boolean _eventChanged = false;

	private static void loadEventList() throws Exception {

		if (_eventList == null || _eventChanged) {
			GenericDal<EventModel, EventModelExt> dalEvent = new GenericDal<EventModel, EventModelExt>(new EventModelExt());
			EventModelExt queryEvent = new EventModelExt();
			queryEvent.setOffset(Integer.MIN_VALUE);
			queryEvent.setLimit(Integer.MIN_VALUE);

			_eventList = dalEvent.selectList(queryEvent);

			_eventChanged = false;
		}
	}

	public static List<EventModel> getEventList() throws Exception {

		loadEventList();

		// TODO : sort

		return _eventList;
	}

	public static List<EventModel> getEventList(boolean sorted, String sortField) throws Exception {
		loadEventList();
		// TODO : sort
		return _eventList;
	}

	public static String getEventName(int idEvent) throws Exception {
		String name = "";

		loadEventList();
		EventModel obj = ModelUtil.<EventModel> getFromList(_eventList, idEvent);
		if (obj != null && obj.getName() != null) {
			name = obj.getName();
		}

		return name;
	}

	// END OF EVENT

}